package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 1.做数据转换，把DTO类转换成实体类
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        // 2.存入自己的用户id，只操作自己的购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 3.在数据库中查询购物车数据（注入shoppingCarMapper层）
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        // 4. 判断是不是新数据，是新数据的话就存入数据库，不是新数据就数量加1
        if (list != null && list.size() > 0) {
            // 说明购物车里有数据，那么旧数据的数量就加一
            shoppingCart = list.get(0);
            Integer number = shoppingCart.getNumber();
            shoppingCart.setNumber(number + 1);
            shoppingCartMapper.update(shoppingCart);
        } else {
            // 4.1 根据给过来的菜品id或者套餐id去查询
            // 4.2 判断dishId是否为空
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                // 如果不为空，说明添加到购物车的数据是菜品
                // 注入菜品的mapper层，根据菜品id找到菜品
                Dish dish = dishMapper.findById(dishId);
                // 把菜品名字、图片、单价、存到购物车中
                //shoppingCart.setName(dish.getName());
                //shoppingCart.setImage(dish.getImage());
                //shoppingCart.setAmount(dish.getPrice());
                //shoppingCart.setCreateTime(LocalDateTime.now());
                //shoppingCart.setNumber(1);
                add(shoppingCart,
                        dish.getName(),
                        dish.getImage(),
                        dish.getPrice(),
                        LocalDateTime.now(),
                        1);
            } else {
                Long setmealId = shoppingCart.getSetmealId();
                // 如果为空，说明添加到购物车的数据是套餐
                // 注入套餐的mapper层,根据套餐id找到套餐
                SetmealVO setmealVO = setmealMapper.findById(setmealId);
                // 把套餐名字、图片、单价存到购物车中
                //shoppingCart.setName(setmealVO.getName());
                //shoppingCart.setImage(setmealVO.getImage());
                //shoppingCart.setAmount(setmealVO.getPrice());
                //shoppingCart.setCreateTime(LocalDateTime.now());
                //shoppingCart.setNumber(1);
                add(shoppingCart,
                        setmealVO.getName(),
                        setmealVO.getImage(),
                        setmealVO.getPrice(),
                        LocalDateTime.now(),
                        1);


            }
        }

    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.clean(shoppingCart);
    }

    /**
     * 删除购物车中的一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 先数据转换
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        // 判断长度是否大于1，大于1做修改，不大于1直接删除
           shoppingCart = list.get(0);
            Integer number = shoppingCart.getNumber();
            if (number > 1){
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.update(shoppingCart);
            }else{
                shoppingCartMapper.clean(shoppingCart);
            }
        }


    // 因为很多数据都是一样的，所以可以把相同的代码提取出来做成一个方法
    private void add(ShoppingCart shoppingCart,
                     String name, String image,
                     BigDecimal Price,
                     LocalDateTime localDateTime,
                     Integer number) {
        shoppingCart.setName(name);
        shoppingCart.setImage(image);
        shoppingCart.setAmount(Price);
        shoppingCart.setCreateTime(localDateTime);
        shoppingCart.setNumber(number);
        // 最后调用购物车的mapper层把数据存入数据库中
        shoppingCartMapper.add(shoppingCart);
    }
}
